<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>订单编号：</label>
            <el-input type="text" clearable placeholder="请输入订单号" v-model="keyword"></el-input>
          </div>
          <div class="operate_item"  v-if="!$route.query.userId">
            <label>用户：</label>
            <userSelect @listenToUser="getUserId" :uId="userId"/>
          </div>
          <div class="operate_item" v-if="!$route.query.productId">
            <label>商品：</label>
            <el-select
              filterable
              clearable
              v-model="productId"
              placeholder="请选择商品">
              <el-option
                v-for="(item,index) in productOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>

<!--          <div class="operate_item">-->
<!--            <label>订单类型：</label>-->
<!--            <el-select-->
<!--              filterable-->
<!--              clearable-->
<!--              v-model="orderType"-->
<!--              placeholder="请选择订单类型">-->
<!--              <el-option-->
<!--                v-for="(item,index) in orderTypeOption"-->
<!--                :key="index"-->
<!--                :label="item.typeName"-->
<!--                :value="item.id">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </div>-->
          <div class="operate_item">
            <label>操作类型：</label>
            <el-select
              filterable
              clearable
              v-model="operateType"
              placeholder="请选择操作类型">
              <el-option
                v-for="(item,index) in operateTypeOption"
                :key="index"
                :label="item.typeName"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <label>操作内容：</label>
            <el-input type="text" clearable placeholder="请输入操作内容" v-model="content"></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="80"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            width="220"
            label="订单号">
            <template slot-scope="scope">{{ scope.row.orderNo ? scope.row.orderNo : '——' }}</template>
          </el-table-column>
          <el-table-column
            prop="wxName"
            label="用户名">
            <template slot-scope="scope">{{ scope.row.wxName ? scope.row.wxName : '——' }}</template>
          </el-table-column>
          <el-table-column
            label="商品名称">
            <template slot-scope="scope">{{ scope.row.productName ? scope.row.productName : '——' }}</template>
          </el-table-column>
          <el-table-column
            width="130"
            label="变更前/后商品数">
            <template slot-scope="scope">
              <p>
                <span style="color: #f7ab20" title="变更前商品数">{{ scope.row.sourceNumbers ? scope.row.sourceNumbers : 0 }}</span> /
                <span style="color: #e40000" title="变更后商品数">{{ scope.row.numbers ? scope.row.numbers : 0 }}</span>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            width="170"
            label="变更前/后已使用额度">
            <template slot-scope="scope">
              <p>
                <span style="color: #f7ab20" title="变更前已使用额度">{{ scope.row.sourceUsedAuthNumber ? scope.row.sourceUsedAuthNumber : 0 }}</span> /
                <span style="color: #e40000" title="变更后已使用额度">{{ scope.row.usedAuthNumber ? scope.row.usedAuthNumber : 0 }}</span>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            width="160"
            prop="sourceReturnNumber"
            label="变更前/后在手商品数">
            <template slot-scope="scope">
              <p>
                <span style="color: #f7ab20" title="变更前在手商品数">{{ scope.row.sourceReturnNumber ? scope.row.sourceReturnNumber : 0 }}</span> /
                <span style="color: #e40000" title="变更后在手商品数">{{ scope.row.returnNumber ? scope.row.returnNumber : 0 }}</span>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作类型">
            <template slot-scope="scope">{{ scope.row.operateTypeName ? scope.row.operateTypeName : '——' }}</template>
          </el-table-column>
          <el-table-column
            prop="content"
            label="操作内容">
          </el-table-column>
          <el-table-column
            label="订单类型">
            <template slot-scope="scope">
              <p v-for="(item,index) in orderTypeOption" :key="index" v-if="scope.row.orderType===item.id">
                {{ item.typeName }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作人">
            <template slot-scope="scope">{{ scope.row.operateUserName ? scope.row.operateUserName : '——' }}</template>
          </el-table-column>
          <el-table-column
            width="160"
            sortable
            prop="modifyTime"
            label="操作时间">
            <template slot-scope="scope">{{ formatTime(scope.row.modifyTime) }}</template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>

import commons from "../../../utils/common";
import {getLog, getOperateType, getOrderType} from "@/api/data/user/product";
import {listAllData} from "@/api/config/product";
import {getBtn} from "@/utils/btnAuth";
import userSelect from '../../../components/user/index'

export default {
  components:{
    userSelect
  },
  data() {
    return {
      keyword: '',
      content: '',
      orderType: '',
      orderTypeOption: '',
      operateType: '',
      operateTypeOption: '',
      userId: '',
      productId: '',
      productOption: '',
      currentPage: 1,
      pageSize: 10,
      totals: 0,
      loading: false,
      tableInfo: [],
      btn: {},
    }

  },
  mounted() {
    this.getOperateTypes();
    this.getOrderTypes();
    this.getProduct();
    //判断这个是否有权限
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }
  },
  methods: {

    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
        orderNo: that.keyword,
        operateTypeId: that.operateType,
        // orderType: that.orderType,
        userId: that.$route.query.userId?that.$route.query.userId:that.userId,
        productId: that.$route.query.productId?that.$route.query.productId:that.productId,
        content: that.content
      }
      getLog(queryParams).then(res => {
        that.tableInfo = res ? res.list : [];
        that.totals = res ? res.count : 0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    // 重置
    resetTable() {
      this.pageSize = 10;
      this.keyword = '';
      this.operateType = '';
      this.orderType = '';
      this.userId = '';
      this.productId = '';
      this.content = '';
      this.getTableInfo(1)
    },

    // 获取用户id
    getUserId(e){
      this.userId = e;
    },

    // 获取订单类型
    getOrderTypes() {
      let that = this;
      getOrderType().then(res => {
        that.orderTypeOption = res ? res : [];
      }).catch((e) => {
        that.orderTypeOption = [];
      });
    },

    // 获取操作类型
    getOperateTypes() {
      let that = this;
      getOperateType().then(res => {
        that.operateTypeOption = res ? res : [];
      }).catch((e) => {
        that.operateTypeOption = [];
      });
    },

    // 获取操作类型
    getProduct() {
      let that = this;
      listAllData().then(res => {
        that.productOption = res ? res : [];
      }).catch((e) => {
        that.productOption = [];
      });
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>

</style>
